package comCS275.note_share;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class UploadPage extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_upload_page);
		final String[] files={"test.txt", "test2.pdf","test3.ppt"};
		final TextView selectedOpt=(TextView)findViewById(R.id.selectedopt);
		final ListView filesList = (ListView)findViewById(R.id.files_list);
		final ArrayAdapter<String> arrayAdpt= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,files);
		filesList.setAdapter(arrayAdpt);
		filesList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
			{
				//filesList.getTag(pos);
				String fileSelected =(String) parent.getItemAtPosition(pos); // name of file selected
				
				// Get file path somehow... using tests for now:
		        String sourcePath = "C:/sample.txt";	//absolute path for source file to be copied
		        String target ="C:/Test/";	//directory where file will be copied

				File fSelected = new File(sourcePath);
				File uploadedFile = new File(target+fileSelected); // create new file for copying to upload
				
				try {
					copy(fSelected, uploadedFile);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} // copy fSelected to uploadedFile;

			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.upload_page, menu);
		return true;
	}
	
	public void copy(File src, File dst) throws IOException {
	    InputStream in = new FileInputStream(src);
	    OutputStream out = new FileOutputStream(dst);

	    // Transfer bytes from in to out
	    byte[] buf = new byte[1024];
	    int len;
	    while ((len = in.read(buf)) > 0) {
	        out.write(buf, 0, len);
	    }
	    in.close();
	    out.close();
	}

}
